﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WmWebSystem.Models;
using WmWebSystem.Service;
using System.IO;

namespace WmWebSystem.DbHelper
{
    public static class ChildProjectHelper
    {
        public static IEnumerable<ChildProject> FindAllChildProjectsTo(string pno) 
        {
            //获得所有子项目
            WmDb mydb = new WmDb();
            var cp = from m in mydb.ChildProjects
                        where m.ProjectNo == pno
                        orderby m.AddTime ascending
                        select m;
            return cp;
        }

        public static IEnumerable<ChildProject> FindAllMusicItems()
        {
            WmDb mydb = new WmDb();
            var cp = from m in mydb.ChildProjects
                     where m.MineType == ".mp3" || m.MineType == ".avi"
                     orderby m.AddTime ascending
                     select m;
            return cp;
        }

        public static void CreateNewToChildProjects(string pno, string name,string fileOldName,string summary, string logo, string delPath, string mineType)
        {
            WmDb mydb = new WmDb();
            DateTime time = DateTime.Now;

            ChildProject child = new ChildProject();
            child.ProjectNo = pno;
            child.ChildName = name;
            child.FileOldName = fileOldName;
            child.Content = summary;
            child.UrlPath = logo;
            child.DelPath = delPath;
            child.AddTime = time;
            child.MineType = mineType.ToLower();
            try
            {
                mydb.ChildProjects.Add(child);
                mydb.SaveChanges();
                //将记录写入日志
                string p1 = "Add new to ChildProject, The ProjectNo:" + pno;
                WriteLog.WriteSysLog(p1);
            }
            catch (FieldAccessException ex)
            {
                //失败，进行回滚操作  记录异常
                File.Delete(delPath);
                WriteLog.WriteSysLog(ex.ToString());
            }

        }

        public static void DeleteFromChildProjects(int id)
        {

            WmDb mydb = new WmDb();
            var music = mydb.ChildProjects.Find(id);
            //string music = pro.ProjectNo;
            string url = music.UrlPath;

            //删除项目
            try
            {
                mydb.ChildProjects.Remove(music);
                mydb.SaveChanges();
                //删除指定文件！
                File.Delete(url);
                //将记录写入日志
                string p1 = "Delete a music from ChildProjects, where Id:" + id;
                WriteLog.WriteSysLog(p1);
            }
            catch (FieldAccessException ex)
            {
                //失败，进行回滚操作  记录异常

                WriteLog.WriteSysLog(ex.ToString());
            }

        }

        public static void AddChildProjectsToFavourites(string pno, string userName) 
        {
            WmDb mydb = new WmDb();
            DateTime date = DateTime.Now;
            Favourite favourite = new Favourite();
            favourite.ChildPrpjectNo = pno;
            favourite.UserName = userName;
            favourite.AddTime = date;
            try
            {
                mydb.Favourites.Add(favourite);
                mydb.SaveChanges();
                //将记录写入日志
                string p1 = "Add new to Favourites, The ChildPrpjectNo:" + pno;
                WriteLog.WriteSysLog(p1);
            }
            catch (FieldAccessException ex)
            {
                //失败，进行回滚操作  记录异常
                WriteLog.WriteSysLog(ex.ToString());
            }
        }

    }
}
